<?php

namespace App\Models\Order;

use App\Models\Common\CommonModel;
use App\Models\Constant\Constant;
use App\Models\Goods\Goods;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Order extends CommonModel
{
    use HasFactory;

    protected $table = 'order';
    protected $appends = ['status_name'];

    public function getStatusNameAttribute() {
        return Constant::ORDER_STATUS_MAP[$this->status]??'未知';
    }

    public function getUser() {
        return $this->hasOne(User::class,'id','user_id');
    }

    public function getGoods() {
        return $this->hasOne(Goods::class,'id','goods_id');
    }

    public function getLst(array $conditions=[]) {
        $records = $this->newQuery();

        if(!empty($conditions['tel'])){
            $records = $records->where('tel',$conditions['tel']);
        }
        if(!empty($conditions['passport'])){
            $records = $records->whereHas('getUser',function ($q) use ($conditions){
               $q->where('passport',$conditions['passport']);
            });
        }
        if(!empty($conditions['name'])){
            $records = $records->whereHas('getUser',function ($q) use ($conditions){
               $q->where('name','like','%'.$conditions['name'].'%');
            });
        }
        if(!empty($conditions['title'])){
            $records = $records->whereHas('getGoods',function ($q) use ($conditions){
               $q->where('title','like','%'.$conditions['title'].'%');
            });
        }

        return $records->orderBy('status')->orderByDesc('id');
    }
}
